package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "v01", "专题课程-分类" })
@CrossOrigin
@RestController
@RequestMapping(value = "/api_yw")
public class YxyLessonsTopicCatalogTController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;
    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @ApiOperation(value = "专题课程-一级分类")
    @RequestMapping(value = "/manage/tec/lessons/topic/category/one.v", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> one(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        String schoolId = getProfile(request, response).getSchoolId();
        schoolId = "402882e75ce5c0ef015ce82911400011";
        String author = getProfile(request, response).getUserNick();
        if (reqModel.clzss == 1) {// 校本
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsSchoolCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsSchoolCatalogRepository.findBySchoolIdAndLevel(schoolId, 1);// 一级目录
                quePage.forEach((YxyLessonsSchoolCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        } else {
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsTopicCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsTopicCatalogRepository.findByLevel(1);// 一级目录
                quePage.forEach((YxyLessonsTopicCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        }
    }

    @ApiOperation(value = "专题课程-二级分类")
    @RequestMapping(value = "/manage/tec/lessons/topic/category/two.v", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> two(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        String schoolId = getProfile(request, response).getSchoolId();
        schoolId = "402882e75ce5c0ef015ce82911400011";
        if (reqModel.clzss == 1) {// 校本
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsSchoolCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsSchoolCatalogRepository.findBySchoolIdAndPidAndLevel(schoolId, reqModel.catalogId,
                        2);// 一级目录
                quePage.forEach((YxyLessonsSchoolCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        } else {
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsTopicCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsTopicCatalogRepository.findByLevelAndPid(2, reqModel.catalogId);// 一级目录
                quePage.forEach((YxyLessonsTopicCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        }
    }

    @ApiOperation(value = "专题课程-三级分类")
    @RequestMapping(value = "/manage/tec/lessons/topic/category/three.v", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> three(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        String schoolId = getProfile(request, response).getSchoolId();
        if (reqModel.clzss == 1) {// 校本
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsSchoolCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsSchoolCatalogRepository.findBySchoolIdAndPidAndLevel(schoolId, reqModel.catalogId,
                        3);// 一级目录
                quePage.forEach((YxyLessonsSchoolCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        } else {
            Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
            List<YxyLessonsTopicCatalog> quePage = null;
            List<RespModel> respModels = new ArrayList<RespModel>();
            try {
                quePage = yxyLessonsTopicCatalogRepository.findByLevelAndPid(3, reqModel.catalogId);// 一级目录
                quePage.forEach((YxyLessonsTopicCatalog item) -> {
                    RespModel rm = new RespModel();
                    rm.setLabel(item.getName());
                    rm.setValue(item.getId());
                    respModels.add(rm);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTopicCatalogTController.RespModel>(respModels, pageable,
                    quePage.size());
            return respPage;
        }
    }

    // 分页请求
    protected static class PageReqModel extends YxyApiPageReqModel {
        public String id; //
        public String pid;
        public String catalogId;
        public Integer clzss; // 1校本 2专题
    }

    protected static class RespModel extends YxyApiRespModel {
        private String label;// 目录
        private String value;// id

        /**
         * @return the label
         */
        public String getLabel() {
            return label;
        }

        /**
         * @param label
         *            the label to set
         */
        public void setLabel(String label) {
            this.label = label;
        }

        /**
         * @return the value
         */
        public String getValue() {
            return value;
        }

        /**
         * @param value
         *            the value to set
         */
        public void setValue(String value) {
            this.value = value;
        }

    }
}
